import scrapy


class FlySpider(scrapy.Spider):
    name = 'fly'
    allowed_domains = ['flight.qunar.com']
    start_urls = ['https://flight.qunar.com/site/index.htm']

    # 重写此方法的目的是为了携带请求头headers
    def start_requests(self):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36 SLBrowser/6.0.1.9171"
        }

        yield scrapy.Request(url=self.start_urls[0], callback=self.first_parse, headers=headers)

    def first_parse(self, response):
        yield scrapy.FormRequest.from_response(
            response,  # 自动的从response中寻找from表单
            formdata={"fromCity": "北京",
                      "toCity": "武汉",
                      "fromDate": " 2020-12-13"},
            callback=self.parse
        )

    def parse(self, response):
        print(response.json())
